<?php
include("template_top.php");
if(isset($_POST['action'])){
	if($_POST['action'] == 'Tech_Out'){
		if($_POST['reason'] == 'Called Out'){
			$sql = "UPDATE `tbTechs` SET `sOutTill` = :return WHERE `iID` = :id";
		}else{
			$sql = "UPDATE `tbTechs` SET `sProjectTill` = :return WHERE `iID` = :id";
		}
		db::Query($sql, array(':return'=>strtotime($_POST['return']), ':id'=>$_POST['tech']));
	}
	
	if($_POST['action'] == 'Employee_Out'){
		$sql = "UPDATE `tbEmployees` SET `sOutTill` = :return WHERE `iID` = :id";
		db::Query($sql, array(':return'=>strtotime($_POST['return']), ':id'=>$_POST['tech']));
	}
}
if(isset($_GET['action'])){
	if($_GET['action'] == 'Tech_In'){
		$sql = "UPDATE `tbTechs` SET `sOutTill` = '0', `sProjectTill` = :return  WHERE `iID` = :id";
		db::Query($sql, array(':return'=>'0', ':id'=>$_GET['id']));
	}
	
	if($_GET['action'] == 'Employee_In'){
		$sql = "UPDATE `tbEmployees` SET `sOutTill` = :return WHERE `iID` = :id";
		db::Query($sql, array(':return'=>0, ':id'=>$_GET['id']));
	}
}
?>
<h1>User Control Center</h1>
<p>
<table width="100%">
	<tr>
    	<td width="50%">
        	<h4>Set Desktop Tech Out</h4>
            <form action="User_Control_Center.php" method="post">
            <center>
	            Select Desktop Tech:<br/>
	            <select name="tech">
	            	<option value="0">-- SELECT ONE --</option>
	                <?php
					$sql = "SELECT * FROM `tbTechs`";
					$q = db::Query($sql);
					while($row = $q->fetch()){
					?>
	                <option value="<?php echo $row['iID'] ?>"><?php echo $row['sTech'] ?></option>
	                <?php } ?>
	            </select><br/><br/>
                Reson For Being Out:<br/>
                <input type="radio" name="reason" value="Called Out" checked="checked" /> Called Out&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="radio" name="reason" value="Working on Project" /> Working on Project<br/><br/>
                Will Return:<br/>
                <input type="text" name="return" value="<?php echo date("n/j/Y", strtotime("+1 day")) ?>" /><br/><br/>
                <input type="hidden" name="action" value="Tech_Out" />
                <input type="submit" value=" Set As Out " />
			</center>
            </form>
            <br/>
            <br/>
        </td>
        <td>
        	<h4>Set Employee Out</h4>
            <form action="User_Control_Center.php" method="post">
            <center>
	            Select Employee:<br/>
	            <select name="tech">
	            	<option value="0">-- SELECT ONE --</option>
	                <?php
					$sql = "SELECT * FROM `tbEmployees` ORDER BY `sLname`";
					$q = db::Query($sql);
					while($row = $q->fetch()){
					?>
	                <option value="<?php echo $row['iID'] ?>"><?php echo $row['sFname']." ".$row['sLname'] ?></option>
	                <?php } ?>
	            </select><br/><br/>
                Reson For Being Out:<br/>
                <input type="radio" name="reason" value="Called Out" checked="checked" /> Called Out<br/><br/>
                Will Return:<br/>
                <input type="text" name="return" value="<?php echo date("n/j/Y", strtotime("+1 day")) ?>" /><br/><br/>
                <input type="hidden" name="action" value="Employee_Out" />
                <input type="submit" value=" Set As Out " />
			</center>
            </form>
            <br/>
            <br/>
        </td>
	</tr>
    <tr>
    	<td>
        	<h4>Desktop Techs Out Today</h4>
            <table width="100%">
            	<tr>
                	<td style="border-bottom:1px #000 solid">Desktop Tech</td>
                    <td style="border-bottom:1px #000 solid">Reason</td>
                    <td style="border-bottom:1px #000 solid">Will Return</td>
                    <td style="border-bottom:1px #000 solid">Actions</td>
                </tr>
            <?php
			$sql = "SELECT * FROM `tbTechs` WHERE `sOutTill` > :t or `sProjectTill` > :r";
			$q = db::Query($sql, array(':t'=>time(), ':r'=>time()));
			while($row = $q->fetch()){
			?>
            	<tr>
                	<td style="border-bottom:1px #000 solid"><?php echo $row['sTech'] ?></td>
                    <td style="border-bottom:1px #000 solid"><?php if($row['sOutTill'] > time()){echo "Called Out";}else{echo "Working on Project";} ?></td>
                    <td style="border-bottom:1px #000 solid"><?php if($row['sOutTill'] > time()){echo date("n/j/Y", $row['sOutTill']);}else{echo date("n/j/Y", $row['sProjectTill']);} ?></td>
                    <td style="border-bottom:1px #000 solid"><a href="User_Control_Center.php?action=Tech_In&id=<?php echo $row['iID'] ?>">Return Early</a></td>
                </tr>
            <?php } ?>
            </table>
        </td>
        <td>
        	<h4>Employees Out Today</h4>
            <table width="100%">
            	<tr>
                	<td style="border-bottom:1px #000 solid">Employee</td>
                    <td style="border-bottom:1px #000 solid">Reason</td>
                    <td style="border-bottom:1px #000 solid">Will Return</td>
                    <td style="border-bottom:1px #000 solid">Actions</td>
                </tr>
            <?php
			$sql = "SELECT * FROM `tbEmployees` WHERE `sOutTill` > :t";
			$q = db::Query($sql, array(':t'=>time()));
			while($row = $q->fetch()){
			?>
            	<tr>
                	<td style="border-bottom:1px #000 solid"><?php echo $row['sFname']." ".$row['sLname'] ?></td>
                    <td style="border-bottom:1px #000 solid"><?php echo "Called Out" ?></td>
                    <td style="border-bottom:1px #000 solid"><?php echo date("n/j/Y", $row['sOutTill']) ?></td>
                    <td style="border-bottom:1px #000 solid"><a href="User_Control_Center.php?action=Employee_In&id=<?php echo $row['iID'] ?>">Return Early</a></td>
                </tr>
            <?php } ?>
            </table>
        </td>
	</tr>
</table>
</p>
<h1>User Account Control Center</h1>
<p>
<table width="100%">
	<tr>
    	<td>
        <h4>Create New Account</h4>
        
        </td>
        <td>
        <h4>Manage User Account</h4>
        </td>
	</tr>
</table>
</p>
<?php
include("template_bot.php");
?>